<template>
    <div class="form-single-fragment" style="position: relative;">
      <el-form ref="formEditOrgPaper" :model="formData" class="full-width-input" :rules="rules" style="width: 100%;"
        label-width="100px" :size="defaultFormItemSize" label-position="right" @submit.native.prevent>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="学生姓名" prop="OrgPaper.studentName">
              <el-input class="input-item" v-model="formData.OrgPaper.studentName"
                        :clearable="true" placeholder="学生姓名">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="OrgPaper.commitTime">
              <el-date-picker class="input-item" v-model="formData.OrgPaper.commitTime" :clearable="true"
                              format="yyyy-MM-dd"
                              value-format="yyyy-MM-dd"
                              type="date" align="left"
                              placeholder="创建时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="校区" prop="OrgPaper.schoolId">
              <el-cascader class="input-item" v-model="formEditOrgPaper.schoolId.value"
                           :options="formEditOrgPaper.schoolId.impl.dropdownList"
                           filterable
                           :clearable="true"
                           :show-all-levels="false"
                           placeholder="校区"
                           :props="{value: 'id', label: 'name', checkStrictly: true}"
                           @visible-change="formEditOrgPaper.schoolId.impl.onVisibleChange"
                           @change="onSchoolIdValueChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-row class="no-scroll flex-box" type="flex" justify="end">
              <el-button type="primary" :size="defaultFormItemSize" :plain="true"
                         @click="onCancel(false)">
                取消
              </el-button>
              <el-button type="primary" :size="defaultFormItemSize"
                         @click="onUpdateClick()">
                保存
              </el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
    </div>
</template>

<script>
/* eslint-disable-next-line */
import { findTreeNode, findTreeNodePath, findItemFromList } from '@/utils';
/* eslint-disable-next-line */
import rules from '@/utils/validate.js';
/* eslint-disable-next-line */
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js';
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin } from '@/core/mixins';
/* eslint-disable-next-line */
import { OrgPaperController, DictionaryController } from '@/api';

    export default {
      name: "formEditOrgPaper",
      props: {
        transId: {
          default: undefined
        }
      },
      mixins: [uploadMixin, statsDateRangeMixin],
      data () {
          return {
            formData: {
              OrgPaper: {
                transId: undefined,
                studentName: undefined,
                commitTime: undefined,
                schoolId: undefined,
                studentId: undefined,
                paperId: undefined,
                exerciseId: undefined,
                deviceType:undefined,
                correctFlag:undefined,
                usedSeconds:undefined,
                isDatasourceInit: false
              }
            },
            rules: {
              'OrgPaper.studentName': [
                {require: true,message: '请输入学生姓名', trigger: 'blur'}
              ],
              'OrgPaper.schoolId':[
                {require: true,message: '请输入所属校区', trigger: 'blur'}
              ],
            },
            formEditOrgPaper: {
              formFilter: {
              },
              formFilterCopy: {
              },
              schoolId: {
                impl: new DropdownWidget(this.loadSchoolIdDropdownList, true, 'id', 'parentId'),
                value: []
              },
              menuBlock: {
                isInit: false
              },
              isInit: false
            }
          }
      },
      methods: {
          onCancel (isSuccess, data) {
            if (this.observer != null) {
              this.observer.cancel(isSuccess, data)
            }
          },
        /**
         * 所属校区下拉数据获取函数
         */
        loadSchoolIdDropdownList () {
            return new Promise(((resolve, reject) => {
              let params = {};
              DictionaryController.dictSysDept(this,params).then(res => {
                resolve(res.getList());
              }).catch(e => {
                reject(e);
              })
            }))
        },
        /**
         * 所属校区选中值改变
         */
        onSchoolIdValueChange (value) {
            this.formData.OrgPaper.schoolId = Array.isArray(value) ? value[value.length - 1] : undefined;
        },
        /**
         * 更新编辑
         */
        refreshFormEditOrgPaper (reloadData = false) {
          this.loadOrgPaperData().then(res => {
            if (!this.formEditOrgPaper.isInit) {
              this.formEditOrgPaper.schoolId.impl.onVisibleChange(true).then(res => {
                this.formEditOrgPaper.schoolId.value = findTreeNodePath(res,this.formData.OrgPaper.schoolId,'id');
              }).catch(e => {});
            }
            this.formEditOrgPaper.isInit = true;
          }).catch(e => {});
        },
        /**
         * 保存
         */
        onUpdateClick () {
          this.$refs.formEditOrgPaper.validate((valid) => {
            if (!valid) return;
            let params = {
              orgPaperDto: {
                transId: this.transId,
                studentName: this.formData.OrgPaper.studentName,
                commitTime: this.formData.OrgPaper.commitTime,
                schoolId: this.formData.OrgPaper.schoolId,
                studentId: this.formData.OrgPaper.studentId,
                paperId: this.formData.OrgPaper.paperId,
                exerciseId: this.formData.OrgPaper.exerciseId,
                deviceType:this.formData.OrgPaper.deviceType,
                correctFlag:this.formData.OrgPaper.correctFlag,
                usedSeconds:this.formData.OrgPaper.usedSeconds,
              }
            };

            OrgPaperController.update(this,params).then(res => {
              this.$message.success('保存成功');
              this.onCancel(true);
            }).catch(e => {});
          });
        },
        /**
         * 获取数据详细信息
         */
        loadOrgPaperData () {
          return new Promise((resolve,reject) => {
            if (!this.formData.OrgPaper.isDatasourceInit) {
              if (this.transId == null){
                this.resetFormData();
                reject();
                return;
              }
              let params = {
                transId: this.transId
              };
              OrgPaperController.view(this,params).then(res => {
                this.formData.OrgPaper = {...res.data, isDatasourceInit: true};
                if (this.formData.OrgPaper.schoolIdDictMap && this.formEditOrgPaper.schoolId.impl.dirty) {
                  this.formEditOrgPaper.schoolId.impl.dropdownList = [this.formData.OrgPaper.schoolIdDictMap]
                }
                if (this.formData.OrgPaper.schoolId) {
                  this.formEditOrgPaper.schoolId.value = [this.formData.OrgPaper.schoolId]
                }
                resolve();
              }).catch(e => {
                reject();
              });
            } else {
              resolve();
            }
          });
        },
        /**
         * 重置表单数据
         */
        resetFormData () {
          this.formData = {
            OrgPaper: {
              transId: undefined,
              studentName: undefined,
              commitTime: undefined,
              schoolId: undefined,
              isDatasourceInit: false
            }
          }
        },
        formInit () {
          this.refreshFormEditOrgPaper();
        }
      },
      computed () {},
      mounted () {
          this.formInit();
      },
      watch: {}
    }
</script>

<style scoped>

</style>
